package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.constant.BasicConstant;
import com.ttg.common.enums.TaskStatusEnum;
import com.ttg.model.dto.open.QueryTaskListDto;
import com.ttg.model.mapper.TaskInfoMapper;
import com.ttg.model.pojo.TaskInfo;
import com.ttg.web.dao.TaskInfoDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TaskInfoDaoImpl extends ServiceImpl<TaskInfoMapper, TaskInfo> implements TaskInfoDao {


    @Override
    public TaskInfo getByTag(String taskTag) {
        LambdaQueryWrapper<TaskInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TaskInfo::getTaskTag,taskTag);
        return getOne(wrapper);
    }

    /**
     * 查询当日前置任务是否已完成
     * @return 未完成前置任务数量
     */

    @Override
    public void updateLastExcuteDateByTag(String taskTag, Integer excuteDate) {
        LambdaUpdateWrapper<TaskInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TaskInfo::getTaskTag,taskTag)
                .eq(TaskInfo::getTaskStatus,TaskStatusEnum.IN_PROGRESS.getStatus())
                .set(TaskInfo::getTaskStatus,TaskStatusEnum.NORMAL.getStatus())
                .set(TaskInfo::getTaskLastExcuteDate,excuteDate);
        update(wrapper);
    }



    @Override
    public void setTaskFail(String taskTag,String message) {
        LambdaUpdateWrapper<TaskInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TaskInfo::getTaskTag,taskTag)
                .set(TaskInfo::getTaskStatus,TaskStatusEnum.FAIL.getStatus())
                .set(TaskInfo::getTaskMessage,message);
        update(wrapper);
    }





    @Override
    public Integer countTodayUnExcuteSameTypeTask(Integer type, Integer excuteDate) {
        LambdaQueryWrapper<TaskInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TaskInfo::getTaskType,type)
                .lt(TaskInfo::getTaskLastExcuteDate,excuteDate);
        return count(wrapper);
    }

    @Override
    public void setTaskNormal(String taskTag) {
        LambdaUpdateWrapper<TaskInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TaskInfo::getTaskTag,taskTag)
                .set(TaskInfo::getTaskStatus,TaskStatusEnum.NORMAL.getStatus());
        update(wrapper);
    }
    @Override
    public void setTaskInProgress(String taskTag) {
        LambdaUpdateWrapper<TaskInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TaskInfo::getTaskTag,taskTag)
                .set(TaskInfo::getTaskStatus,TaskStatusEnum.IN_PROGRESS.getStatus());
        update(wrapper);
    }


}
